﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OFB.BLL.Repository;
using OFB.Domain;
using OFB.BLL.Context;
using System.Data;
using OFB.BLL.ViewModel;

namespace OFB.BLL.Repository
{
    public class ManageAdminBLL : IManageAdminBLL
    {
        public ManageAdminContext context;

        public ManageAdminBLL() 
        {
            context = new ManageAdminContext();
        }

        public AdminListViewModel FindAll(int page = 0, int statusId = 2)
        {
            IDataParameter totalItemParam = context.ManageAdmin.BuildParameter("TotalRow", DbType.Int32, ParameterDirection.Output);
            IDataParameter pageParam = context.ManageAdmin.BuildParameter("Page", DbType.Int32, page);
            IDataParameter statusParam = context.ManageAdmin.BuildParameter("FindBy", DbType.Int32, statusId);

            AdminListViewModel vm = new AdminListViewModel();
            vm.Admins = context.ManageAdmin.Init("Admin_FindAll", totalItemParam, pageParam, statusParam)
                                         .MapCollection<AdminViewModel>()
                                         .GetCollection();
            vm.TotalItem = (int)totalItemParam.Value;

            return vm;
        }

        public int AdminUpdate(int AdminId, string Username, string Password, string Name, string Phone, string Email)
        {
            IDataParameter rowEffectedParam = context.ManageAdmin.BuildParameter("RowCount", DbType.Int32, ParameterDirection.Output);
            context.ManageAdmin.Execute("Admin_Update", context.ManageAdmin.BuildParameter("AdminId", DbType.Int32, AdminId),
                                                       context.ManageAdmin.BuildParameter("Username", DbType.String, Username),
                                                       context.ManageAdmin.BuildParameter("Password", DbType.String, Password),
                                                       context.ManageAdmin.BuildParameter("Fullname", DbType.String, Name),
                                                       context.ManageAdmin.BuildParameter("Phone", DbType.String, Phone),
                                                       context.ManageAdmin.BuildParameter("Email", DbType.String, Email),
                                                    rowEffectedParam);
            return (int)rowEffectedParam.Value;
        }

        public int AdminUpdateStatus(string username, bool isDisabled)
        {
            IDataParameter usernameParam = context.ManageAdmin.BuildParameter("Username", DbType.String, username);
            IDataParameter isDisabledParam = context.ManageAdmin.BuildParameter("IsDisabled", DbType.Boolean, isDisabled);
            IDataParameter rowEffectedParam = context.ManageAdmin.BuildParameter("RowEffected", DbType.Int32, ParameterDirection.Output);

            context.ManageAdmin.Execute("Admin_ChangeStatus", usernameParam, isDisabledParam, rowEffectedParam);

            return (int)rowEffectedParam.Value;
        }

        public AdminViewModel Find(int AdminId)
        {
            return context.ManageAdmin.Init("Admin_Find", context.ManageAdmin.BuildParameter("AdminId", System.Data.DbType.Int32, AdminId))
                                        .Map()
                                        .Get();
        }

        public AdminViewModel FindByUsername(string userName)
        {
            return context.ManageAdmin.Init("Admin_FindByUsername", context.ManageAdmin.BuildParameter("Username", DbType.String, userName))
                                                .Map()
                                                .Get();
        }

        public int AdminCreate(AdminViewModel admin)
        {
            IDataParameter manageAdminIdParam = context.ManageAdmin.BuildParameter("Identity", DbType.Int32, ParameterDirection.Output);
            context.ManageAdmin.Execute("Admin_Create", context.ManageAdmin.BuildParameter("Username", DbType.String, admin.Username),
                                                       context.ManageAdmin.BuildParameter("Password", DbType.String, admin.Password),
                                                       context.ManageAdmin.BuildParameter("Fullname", DbType.String, admin.Fullname),
                                                       context.ManageAdmin.BuildParameter("Phone", DbType.String, admin.Phone),
                                                       context.ManageAdmin.BuildParameter("Email", DbType.String, admin.Email),
                                                       context.ManageAdmin.BuildParameter("CreatedDate", DbType.DateTime, admin.CreatedDate),
                                                       manageAdminIdParam);
            return (int)manageAdminIdParam.Value;
        }

        public AdminListViewModel FindByName(string name, int page = 0, int findBy = 2)
        {
            IDataParameter totalItemParam = context.ManageAdmin.BuildParameter("TotalRow", DbType.Int32, ParameterDirection.Output);
            IDataParameter adminNameParam = context.ManageAdmin.BuildParameter("Name", DbType.String, name);
            IDataParameter pageParam = context.ManageAdmin.BuildParameter("Page", DbType.Int32, page);
            IDataParameter findByParam = context.ManageAdmin.BuildParameter("FindBy", DbType.Int32, findBy);

            AdminListViewModel vm = new AdminListViewModel();
            vm.Admins = context.ManageAdmin.Init("Admin_FindByName", adminNameParam, totalItemParam, pageParam, findByParam)
                                         .MapCollection<AdminViewModel>()
                                         .GetCollection();
            vm.TotalItem = (int)totalItemParam.Value;

            return vm;
        }

        public AdminListViewModel FindByAccount(string account, int page = 0, int findBy = 2)
        {
            IDataParameter totalItemParam = context.ManageAdmin.BuildParameter("TotalRow", DbType.Int32, ParameterDirection.Output);
            IDataParameter accountParam = context.ManageAdmin.BuildParameter("Account", DbType.String, account);
            IDataParameter pageParam = context.ManageAdmin.BuildParameter("Page", DbType.Int32, page);
            IDataParameter findByParam = context.ManageAdmin.BuildParameter("FindBy", DbType.Int32, findBy);

            AdminListViewModel vm = new AdminListViewModel();
            vm.Admins = context.ManageAdmin.Init("Admin_FindByAccount", accountParam, totalItemParam, pageParam, findByParam)
                                         .MapCollection<AdminViewModel>()
                                         .GetCollection();
            vm.TotalItem = (int)totalItemParam.Value;

            return vm;
        }

        public AdminListViewModel FindByEmail(string email, int page = 0, int findBy = 2)
        {
            IDataParameter totalItemParam = context.ManageAdmin.BuildParameter("TotalRow", DbType.Int32, ParameterDirection.Output);
            IDataParameter emailParam = context.ManageAdmin.BuildParameter("Email", DbType.String, email);
            IDataParameter pageParam = context.ManageAdmin.BuildParameter("Page", DbType.Int32, page);
            IDataParameter findByParam = context.ManageAdmin.BuildParameter("FindBy", DbType.Int32, findBy);

            AdminListViewModel vm = new AdminListViewModel();
            vm.Admins = context.ManageAdmin.Init("Admin_FindByEmail", emailParam, totalItemParam, pageParam, findByParam)
                                         .MapCollection<AdminViewModel>()
                                         .GetCollection();
            vm.TotalItem = (int)totalItemParam.Value;

            return vm;
        }

        public AdminListViewModel FindByFindAll(string searchString, int page = 0, int findBy = 2)
        {
            IDataParameter totalItemParam = context.ManageAdmin.BuildParameter("TotalRow", DbType.Int32, ParameterDirection.Output);
            IDataParameter searchStringParam = context.ManageAdmin.BuildParameter("SearchString", DbType.String, searchString);
            IDataParameter pageParam = context.ManageAdmin.BuildParameter("Page", DbType.Int32, page);
            IDataParameter findByParam = context.ManageAdmin.BuildParameter("FindBy", DbType.Int32, findBy);

            AdminListViewModel vm = new AdminListViewModel();
            vm.Admins = context.ManageAdmin.Init("Admin_FindByFindAll", searchStringParam, totalItemParam, pageParam, findByParam)
                                         .MapCollection<AdminViewModel>()
                                         .GetCollection();
            vm.TotalItem = (int)totalItemParam.Value;

            return vm;
        }

        #region IManageAdminBLL Members


        public bool IsSuperAdmin(string Username)
        {
            IDataParameter UserName = context.ManageAdmin.BuildParameter("Username", DbType.String, Username);
            IDataParameter RowEffect = context.ManageAdmin.BuildParameter("RowEffect", DbType.Int32, ParameterDirection.Output);
            AdminViewModel vm = new AdminViewModel();
            vm = context.ManageAdmin.Init("Admin_IsSuperAdmin", UserName, RowEffect)
                .Map()
                .Get();
            if (Convert.ToInt32(RowEffect.Value) > 0)
            {
                return true;
            }
            return false;
        }

        #endregion
    }
}
